package com.drcoffee.drcoffeemall.mapper;

import com.drcoffee.drcoffeemall.entry.BsPurview;
import com.drcoffee.drcoffeemall.entry.BsPurviewType;
import com.drcoffee.drcoffeemall.entry.BsRole;
import com.drcoffee.drcoffeemall.entry.BsUser;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Mapper
@Repository
public interface BsUserMapper {
    /* 登录校验 */
    @Select("select * from t_bs_user where account = #{account} and password = #{password} and status = 'Y'")
    BsUser login(BsUser bsUser);

    /* 根据账号查找用户 */
    @Select("select * from t_bs_user where account = #{account} and status = 'Y'")
    BsUser getBsUserByAccount(String account);


    @Select("SELECT * FROM t_bs_user WHERE account LIKE #{search} OR username LIKE #{search}")
    List<BsUser> getBsUser(String search);

    @Select("SELECT b.* FROM t_bs_user_m_role a LEFT JOIN t_bs_role b on a.role_id = b.id WHERE a.user_id = #{id}")
    List<BsRole> getBsRoleByUser(Integer id);


    @Select("SELECT c.* FROM t_bs_role a JOIN t_bs_role_m_purview b ON a.id = b.role_id LEFT JOIN t_bs_purview c ON b.purview_id = c.id WHERE a.id = #{role_id}")
    List<BsPurview> getBsPurviewByRole(Integer role_id);

    //    @Select("SELECT * FROM t_bs_purview_type WHERE id IN(SELECT DISTINCT c.type_id FROM t_bs_role a JOIN t_bs_role_m_purview b ON a.id = b.role_id LEFT JOIN t_bs_purview c ON b.purview_id = c.id WHERE a.id = #{role_id})")
//    List<BsPurviewType> getBsPurviewType(Integer role_id);

    List<BsPurviewType> getBsPurviewType(List<BsRole> roles);

    @Select("SELECT * FROM  t_bs_role")
    List<BsRole> getAllBsRole();

    @Select("SELECT * FROM  t_bs_purview")
    List<BsPurview> getAllBsPurview();


    @Insert("INSERT INTO t_bs_role (role_name) VALUES (#{role_name})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    void addRole(BsRole bsRole);

    void addRolePurview(Integer new_role_id, List<Integer> purview_ids);

    @Delete("DELETE FROM t_bs_role_m_purview WHERE role_id = #{role_id}")
    void deleteRolePurview(Integer role_id);

    @Delete("DELETE FROM t_bs_user_m_role WHERE role_id = #{role_id}")
    void deleteRoleUser(Integer role_id);

    @Delete("DELETE FROM t_bs_user_m_role WHERE user_id = #{user_id}")
    void deleteUserRole(Integer user_id);

    @Delete("DELETE FROM t_bs_role WHERE id = #{role_id}")
    void deleteRole(Integer role_id);

    @Insert("INSERT INTO t_bs_user(account,password,username,email,status) VALUES (#{account},#{password},#{username},#{email},#{status})")
    void addBsUser(BsUser bsUser);

    @Delete("DELETE FROM t_bs_user_m_role WHERE user_id = #{id}")
    void deleteUserRoleByUserId(Integer id);

    @Delete("DELETE FROM t_bs_user WHERE id = #{id}")
    void deleteBsUser(Integer id);

    @Select("SELECT count(*) FROM t_bs_user WHERE account = #{account}")
    Integer getBsUserByAccountCount(String account);

    @Update("UPDATE t_bs_user SET account = #{account},username = #{username},password = #{password},email=#{email},status=#{status} WHERE id = #{id}")
    void editBsUser(BsUser bsUser);

    @Insert("INSERT INTO t_bs_purview (purview_name,purview_path,type_id) VALUES (#{purview_name},#{purview_path},#{type_id})")
    void addBsPurview(BsPurview bsPurview);

    @Update("UPDATE t_bs_purview SET purview_name = #{purview_name},purview_path = #{purview_path} ,type_id = #{type_id} WHERE id = #{id}")
    void editBsPurview(BsPurview bsPurview);

    @Delete("DELETE FROM t_bs_purview WHERE id = #{id}")
    void deleteBsPurview(Integer id);

    @Select("SELECT * FROM t_bs_purview_type")
    List<BsPurviewType> getAllBsPurviewType();


    @Insert("INSERT INTO t_bs_purview_type (purview_type,icon) VALUES (#{purview_type},#{icon})")
    void addBsPurviewType(BsPurviewType bsPurviewType);

    @Update("UPDATE t_bs_purview_type SET purview_type = #{purview_type},icon = #{icon} WHERE id = #{id}")
    void editBsPurviewType(BsPurviewType bsPurviewType);

    @Select("SELECT COUNT(*) FROM t_bs_purview WHERE type_id = #{id}")
    Integer getBsPurviewByType(Integer id);

    @Delete("DELETE FROM t_bs_purview_type WHERE id = #{id}")
    void deleteBsPurviewType(Integer id);

    void addUserRole(Integer userId, List<Integer> roleIds);

    @Select("SELECT qw_user_id FROM t_bs_user WHERE account in (SELECT bs_user_account FROM t_bs_user_warn WHERE schedule = #{schedule})")
    List<String> getWxWorkUserId(Integer schedule);

    @Update("UPDATE t_bs_role SET role_name = #{roleName} WHERE id = #{roleId}")
    void updateRolePurview(Integer roleId, String roleName);

    @Insert("INSERT INTO t_operation_record VALUES (#{username},NOW(),#{records})")
    void putOperationRecord(String username, String records);
}
